package com.example.demo.service;

import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @auther sc
 * @Date 2017/10/30
 */
@Service
public class CommonService {

    @Autowired
    private RestTemplate restTemplate;


    /***
     * 如果不配置commandProperties默认在不同线程中执行  Thread
     * 配置SEMAPHORE则在同一个线程中执行
     * @return
     */
    @HystrixCommand(fallbackMethod = "commonFallback",commandProperties ={
            @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"),
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    public String foo1(){
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://provide/hh?name={1}",String.class,"shuchang");
        return responseEntity.getBody();
    }

    public String commonFallback(Throwable e){
        return e.getMessage()+"检测到错误发生,进入服务降级-------------------------";
    }

}
